//
//  PersonInfoViewController.m
//  CityFire
//
//  Created by Liuyz on 2017/8/4.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "PersonInfoViewController.h"
#import "PersonInfo1TableViewCell.h"
#import "PersonInfo2TableViewCell.h"
#import "PersonInfo3TableViewCell.h"


#define B(title,s,h, r) [ItemModel modelWithTitle:title height:h content:s ploceHold:r]

#define Compare(s1,s2) [s1 isEqualToString:s2]


typedef NS_ENUM(NSUInteger, PersonInfoType) {
    PersonInfoTypeUserName = 9888,
    PersonInfoTypeFHZRR,
    PersonInfoTypeDUTYID,
    PersonInfoTypeDUTY,
    PersonInfoTypeID,
    PersonInfoTypeGWJB,
    PersonInfoTypeJGPX,
    PersonInfoTypeEmail,
    PersonInfoTypeCEmail,
    PersonInfoTypeUserType,
    PersonInfoTypeSSXQ,
    PersonInfoTypeYHDZ,
    PersonInfoTypeLXR,
    PersonInfoTypeLink1,
    PersonInfoTypePost1,
    PersonInfoTypeName1,
    PersonInfoTypeTel1,
    PersonInfoTypeLink2,
    PersonInfoTypePost2,
    PersonInfoTypeName2,
    PersonInfoTypeTel2,
    PersonInfoTypeLink3,
    PersonInfoTypePost3,
    PersonInfoTypeName3,
    PersonInfoTypeTel3,
};



#define BB(title,s,h, r, t) [ItemModel modelWithTitle:title height:h content:s ploceHold:r type:t]

#define Compare(s1,s2) [s1 isEqualToString:s2]

@interface ItemModel : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, strong) NSString *placeStr;
//@property (nonatomic, assign) CGFloat cellHeight;
@property (nonatomic, assign) PersonInfoType type;
@property (nonatomic, assign) BOOL isRequired;   //必填项
@property (nonatomic, assign) BOOL isTransform;  //是否旋转
@property (nonatomic, assign) BOOL isCheckContent;  //是否选择联系人


@end


@implementation ItemModel

+ (instancetype)modelWithTitle:(NSString *)title height:(CGFloat)height content:(NSString *)content ploceHold:(NSString *)placeHold type:(PersonInfoType)type
{
    ItemModel *model = [[ItemModel alloc] init];
    model.title = title;
    //    model.cellHeight = h;
    model.content = content;
    model.placeStr = placeHold;
    model.type = type;
    
    //    if (model.type == ItemTypeInvestigation2 || model.type == ItemTypeInvestigation3 || model.type == ItemTypeMail) {
    //        model.isRequired = NO;
    //    }else {
    //        model.isRequired = YES;
    //    }
    //
    //    if (model.type == ItemTypeInvestigation1 || model.type == ItemTypeInvestigation2 || model.type == ItemTypeInvestigation3 || model.type == ItemTypeSex || model.type == ItemTypeBirth) {
    //        model.isTransform = YES;
    //    }else {
    //        model.isTransform = NO;
    //    }
    //
    //    model.isCheckContent = (model.type == ItemTypeContactsName) ? YES : NO;
    
    return model;
}

@end


@interface PersonInfoViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) IBOutlet UITableView *personInfoTableView;
@property (nonatomic, weak) IBOutlet UIButton    *updateBtn;
@property (nonatomic, strong) NSArray *personInfoArray;

@end

@implementation PersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //     self.personInfoArray = @[@[@[@"用户姓名", @"测试"], @[@"防火负责人", @"测试0001"], @[@"用户姓名", @"测试"], @[@[@[@"职务", @"测试001"], @[@"工号", @"测试001"]]], @[@"岗位级别", @"测试"], @[@"机构内排序", @"测试"]], @[@[@"私人邮箱", @"测试"], @[@"公司邮箱", @"测试"]], @[@[@"用户姓名", @"测试"], @[@"用户类别", @"测试"], @[@"所属辖区", @"测试"], @[@"用户地址", @"测试"]], @[@"联系人", @"xxxx"], @[@"用户密码", @"测试0001"], @[@"是否启用", @"测试0001"]];
    
    self.title = @"个人信息";
    
    [self bindRAC];
    
}

- (void)bindRAC {
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainW, 80)];
    
    UIButton *footerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    footerBtn.frame = CGRectMake(mainW/2-60, 20, 120, 35);
    footerBtn.backgroundColor = [UIColor whiteColor];
    [footerBtn setTitle:@"更新个人信息" forState:UIControlStateNormal];
    [footerBtn setBackgroundImage:[UIImage imageNamed:@"button_bg_blue_pre"] forState:UIControlStateNormal];
    [footerBtn setTitleColor:[UIColor whiteColor] forState:(UIControlState)UIControlStateNormal];
    [footerBtn addTarget:(self) action:@selector(updatePersonInfomation:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [footerView addSubview:footerBtn];
    self.personInfoTableView.tableFooterView =footerView;
    
    
    [RACObserve(self, personInfoModel) subscribeNext:^(UserInfoModel *infoModel) {
        if (infoModel) {
            
            NSArray *section0 = [NSArray arrayWithObjects:BB(@"用户姓名", infoModel.chnUserName, 0, @"", PersonInfoTypeUserName), BB(@"防火负责人", infoModel.fhzrr, 0, @"", PersonInfoTypeFHZRR), @[@[BB(@"职务", infoModel.duty, 0, @"", PersonInfoTypeDUTY), BB(@"工号", infoModel.userID, 0, @"", PersonInfoTypeID)], @(PersonInfoTypeDUTYID)], BB(@"岗位级别", infoModel.stationLevel, 0, @"", PersonInfoTypeGWJB), BB(@"机构内排序", infoModel.numinDept, 0, @"", PersonInfoTypeJGPX), nil];
            
            NSArray *section1 = [NSArray arrayWithObjects:BB(@"私人邮箱", infoModel.email, 0, @"", PersonInfoTypeEmail), BB(@"公司邮箱", infoModel.emailName, 0, @"", PersonInfoTypeCEmail), nil];
            NSArray *section2 =  [NSArray arrayWithObjects:BB(@"用户类别", infoModel.USERTYPE, 0, @"", PersonInfoTypeUserType), BB(@"所属辖区", infoModel.ssxq, 0, @"", PersonInfoTypeSSXQ), BB(@"用户地址", infoModel.address, 0, @"", PersonInfoTypeYHDZ), nil];
            NSArray *section3 =  [NSArray arrayWithObjects:BB(@"联系人", @"", 0, @"", PersonInfoTypeLXR), @[@[BB(@"职位一", infoModel.post1, 0, @"", PersonInfoTypePost1), BB(@"姓名", infoModel.name1, 0, @"", PersonInfoTypeName1), BB(@"联系电话", infoModel.tel1, 0, @"", PersonInfoTypeTel1)], @(PersonInfoTypeLink1)], @[@[BB(@"职位二", infoModel.post2, 0, @"", PersonInfoTypePost2), BB(@"姓名", infoModel.name2, 0, @"", PersonInfoTypeName2), BB(@"联系电话", infoModel.tel2, 0, @"", PersonInfoTypeTel2)], @(PersonInfoTypeLink2)], @[@[BB(@"职位三", infoModel.post3, 0, @"", PersonInfoTypePost3), BB(@"姓名", infoModel.name3, 0, @"", PersonInfoTypeName3), BB(@"联系电话", infoModel.tel3, 0, @"", PersonInfoTypeTel3)], @(PersonInfoTypeLink3)], nil];
            
            self.personInfoArray = @[section0, section1, section2, section3];
            //            self.personInfoArray = @[@[@[@"用户姓名", infoModel.CHNUSERNAME, @(PersonInfoTypeUserName)], @[@"防火负责人", infoModel.FHZRR, @(PersonInfoTypeFHZRR)], @[@[@[@"职务", infoModel.DUTY, @(PersonInfoTypeDUTY)], @[@"工号", infoModel.USERID, @(PersonInfoTypeID)]], @(PersonInfoTypeDUTYID)], @[@"岗位级别", infoModel.STATIONLEVEL, @(PersonInfoTypeGWJB)], @[@"机构内排序", infoModel.NUMINDEPT, @(PersonInfoTypeJGPX)]],
            //                                     @[@[@"私人邮箱", infoModel.EMAIL, @(PersonInfoTypeEmail)], @[@"公司邮箱", infoModel.EMAILNAME, @(PersonInfoTypeCEmail)]],
            //  @[@[@"用户类别", infoModel.USERTYPE, @(PersonInfoTypeUserType)], @[@"所属辖区", infoModel.SSXQ], @[@"用户地址", infoModel.ADDRESS, @(PersonInfoTypeYHDZ)]],
            
            //  @[@[@"联系人", @"", @(PersonInfoTypeLXR)], @[@[@[@"职位一", infoModel.POST1, @(PersonInfoTypePost1)], @[@"姓名", infoModel.NAME1, @(PersonInfoTypeName1)], @[@"联系电话", infoModel.TEL1, @(PersonInfoTypeTel1)]], @(PersonInfoTypeLink1)], @[@[@[@"职位二", infoModel.POST2, @(PersonInfoTypePost2)], @[@"姓名", infoModel.NAME2, @(PersonInfoTypeName2)], @[@"联系电话", infoModel.TEL2, @(PersonInfoTypeTel2)]], @(PersonInfoTypeLink2)], @[@[ @[@"职位三", infoModel.POST3, @(PersonInfoTypePost3)], @[@"姓名", infoModel.NAME3, @(PersonInfoTypeName3)], @[@"联系电话", infoModel.TEL3, @(PersonInfoTypeTel3)]], @(PersonInfoTypeLink3)]]];
            
            [self.personInfoTableView reloadData];
        }
    }];
}

//update
-(void)updatePersonInfomation:(UIButton *) button{
    NSLog(@"%@",button);
    
    NSDictionary * parameter = @{@"fhzrr":@"lisi",@"sessionid" : @"1"};
    
    [YQNetworking requestWithUrl:kLOGIN_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:parameter progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response, NSError *responseInfo) {
        NSError *error;
        
        if (responseInfo.code == 0) {
            [SVProgressHUD showSuccessWithStatus:@"更新成功"];
        }else {
            [SVProgressHUD showErrorWithStatus:responseInfo.domain];
        }
       
    } failBlock:^(NSError *error) {
         [SVProgressHUD showSuccessWithStatus:@"更新失败"];
    }];
    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.personInfoArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.personInfoArray[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 9;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonInfoType type = self.personInfoArray.count ? [self.personInfoArray[indexPath.section][indexPath.row] isKindOfClass:[NSArray class]] ? [self.personInfoArray[indexPath.section][indexPath.row][1] integerValue] : 0 : 0;
    switch (type) {
        case PersonInfoTypeDUTYID:
        {
            return 50;
        }
            break;
        case PersonInfoTypeLink1: case PersonInfoTypeLink2: case PersonInfoTypeLink3:
        {
            return 74.0f;
        }
            break;
        default:
        {
            return 50.0f;
        }
            break;
    }
    
    return 44.0f;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PersonInfoType type = self.personInfoArray.count ? [self.personInfoArray[indexPath.section][indexPath.row] isKindOfClass:[NSArray class]] ? [self.personInfoArray[indexPath.section][indexPath.row][1] integerValue] : 0 : 0;
    switch (type) {
        case PersonInfoTypeDUTYID:
        {
            PersonInfo2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInfo2TableViewCell"];
            ItemModel *model1 = self.personInfoArray[indexPath.section][indexPath.row][0][0];
            ItemModel *model2 = self.personInfoArray[indexPath.section][indexPath.row][0][1];
            cell.aTitleLabel.text = model1.title;
            cell.aTextField.text = model1.content;
            
            cell.aaTitleLabel.text = model2.title;
            cell.aaTextField.text = model2.content;
            
            return cell;
            
        }
            break;
        case PersonInfoTypeLink1: case PersonInfoTypeLink2: case PersonInfoTypeLink3:
        {
            PersonInfo3TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInfo3TableViewCell"];
            ItemModel *model1 = self.personInfoArray[indexPath.section][indexPath.row][0][0];
            ItemModel *model2 = self.personInfoArray[indexPath.section][indexPath.row][0][1];
            ItemModel *model3 = self.personInfoArray[indexPath.section][indexPath.row][0][2];
            cell.aTitleLabel.text = model1.title;
            cell.aTextField.text = model1.content;
            
            cell.aSub1TitleLabel.text = model2.title;
            cell.aSub1TextField.text = model2.content;
            cell.aSub2TitleLabel.text = model3.title;
            cell.aSub2TextField.text = model3.content;
            
            return cell;
        }
            break;
        default:
        {
            PersonInfo1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInfo1TableViewCell"];
            ItemModel *model = self.personInfoArray[indexPath.section][indexPath.row];
            
            cell.aTitleLabel.text = model.title;
            cell.infoTextField.text = model.content;
            cell.checkSwitch.hidden = YES;
            
            return cell;
            
        }
            break;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
